package cn.edu.hznu.web.crawl;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import org.apache.http.ParseException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CrawlByJsoup {
	
	private final static String FILEDIR = "/Users/LVZY/Documents/Record/Crawl/douban.com/Jsoup/";
	
	public void crawl() {
		String responseBody;
		try {
			responseBody = DownloadContent.getStrContentFromUrl(
					"http://book.douban.com", "utf-8");
			Document indexDoc = Jsoup.parse(responseBody);
			Element articleElm = indexDoc.select("div.article").first();
			this.processBooksExpress(articleElm.getElementsByClass("books-express")
					.first());
			// Other parts of the page to be crawled
		} catch (ParseException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void processBooksExpress(Element booksExpressElm) {
		File booksFolder = this.createFolder(FILEDIR + "express-books/");
		try (BufferedWriter bfWriter = this.createBufferedWriter(
				booksFolder + "/books-details.txt", "utf-8")) {
			booksExpressElm.getElementsByTag("ul").forEach(booksBlockElm -> {
				booksBlockElm.getElementsByTag("li").forEach(bookElms -> {
					try {
						this.writeBookDetails(bfWriter, 
								bookElms.getElementsByClass("more-meta")
							    		.first()
							    		.getElementsByAttribute("class"));
						this.writeBookImg(booksFolder, 
								bookElms.getElementsByClass("cover")
										.first()
										.getElementsByTag("img")
										.first());
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				});
			});
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private BufferedWriter createBufferedWriter(String filename, String chatset)
			throws UnsupportedEncodingException, FileNotFoundException {
		return new BufferedWriter(
					new OutputStreamWriter(
							new FileOutputStream(filename), chatset));
	}
	
	private File createFolder(String folderName) {
		File folder = new File(folderName);
		if (!folder.exists()) {
			folder.mkdirs();
		}
		return folder;
	}
	
	private void writeBookDetails(BufferedWriter bfWriter, Elements bookDetailsElms)
			throws IOException {
		bookDetailsElms.remove(0);
		for (Element book : bookDetailsElms) {
			bfWriter.write(book.className() + ": " + book.text());
			bfWriter.newLine();
		}
		bfWriter.newLine();
	}
	
	private void writeBookImg(File imgFolder, Element bookImgElm) 
			throws FileNotFoundException, IOException {
		try (BufferedOutputStream bfOutStream = 
				new BufferedOutputStream(
					new FileOutputStream(
						new File(imgFolder, bookImgElm.attr("alt") + ".jpg")))) {
			bfOutStream.write(DownloadContent
					.getByteContentFromUrl(bookImgElm.attr("src")));
		}
	}

}
